/*

Miranda IM: the free IM client for Microsoft* Windows*

Copyright 2000-2007 Miranda ICQ/IM project, 
all portions of this codebase are copyrighted to the people 
listed in contributors.txt.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or ( at your option ) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
aLONG with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/

/**
 * System Includes:
 **/
#include "commonheaders.h"
#include "ctrl.h"
#include "pspAll.h"

/**
 * name:	DlgProc_pspEdit()
 * desc:	dialog procedure
 *
 * return:	0 or 1
 **/
static INT CALLBACK DlgProc_pspEdit( HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam, const CHAR* szSetting )
{
	switch( uMsg ) {
		case WM_INITDIALOG:
		{
			HFONT hBold;
			if( !mir_stricmp( szSetting, SET_CONTACT_MYNOTES ) )
				SetDlgItemText( hDlg, TXT_ABOUT, LPGENT( "My Notes:" ) );
			else
				SetDlgItemText( hDlg, TXT_ABOUT, LPGENT( "About:" ) );

			TranslateDialogDefault( hDlg );

			// set bold font for name in description area and listfont
			if( PSGetBoldFont( hDlg, hBold ) )
				SendDlgItemMessage( hDlg, TXT_ABOUT, WM_SETFONT, ( WPARAM )hBold, 0 );
			
			SendDlgItemMessage( hDlg, EDIT_ABOUT, EM_SETEVENTMASK, 0, /*ENM_KEYEVENTS|*/ENM_LINK|ENM_CHANGE );
			SendDlgItemMessage( hDlg, EDIT_ABOUT, EM_AUTOURLDETECT, TRUE, NULL );
			if( !lParam ) SendDlgItemMessage( hDlg, EDIT_ABOUT, EM_LIMITTEXT, 1024, NULL );

			return TRUE;
		}

		case WM_CTLCOLORSTATIC:
			switch( GetWindowLong( ( HWND )lParam, GWL_ID ) ) {
				case EDIT_ABOUT:
					return ( BOOL )GetSysColor( COLOR_WINDOW );
			}
			return FALSE;

		case WM_NOTIFY:
			switch( (( LPNMHDR )lParam )->idFrom ) {
				/**
				 * handles the messages to save and read settings
				 **/
				case 0:
				{
					HANDLE hContact = ( HANDLE )( ( LPPSHNOTIFY )lParam )->lParam;
					LPCSTR szBaseProto;
					
					switch( (( LPNMHDR )lParam )->code ) {
						// some account data may have changed so reread database
						case PSN_INFOCHANGED:
						{
							BYTE bChanged = 0;
							if( !PSGetBaseProto( hDlg, szBaseProto ) || *szBaseProto == 0 ) break;
							bChanged |= RichEdit_SetValue( hDlg, EDIT_ABOUT, hContact, USERINFO, szBaseProto, szSetting );
							SetWindowLongPtr( hDlg, DWL_MSGRESULT, bChanged ? PSP_CHANGED : 0 );
							break;
						}
						// user selected to apply settings to the database
						case PSN_APPLY:
							if( !PSGetBaseProto( hDlg, szBaseProto ) || *szBaseProto == 0 ) break;
							RichEdit_WriteValue( hDlg, EDIT_ABOUT, hContact, USERINFO, szBaseProto, szSetting );
							break;
					}
					break;
				}

				/**
				 * notification handler for richedit control
				 **/
				case EDIT_ABOUT:
				{
					switch( (( LPNMHDR )lParam )->code ) {

						/**
						 * notification handler for a link within the richedit control
						 **/
						case EN_LINK:
							return Edit_LinkNotificationHandler( hDlg, ( ENLINK* )lParam );
					}
					break;
				}
			}
			break; /* case WM_NOTIFY: */

		case WM_COMMAND:
		{
			switch( LOWORD( wParam ) ) {
				case EDIT_ABOUT:
					if( HIWORD( wParam ) == EN_CHANGE )
						RichEdit_Changed( hDlg, EDIT_ABOUT, USERINFO, szSetting );
					break;
			}
			break;
		}

		case WM_DESTROY:
			Edit_Destroy( hDlg, EDIT_ABOUT );
			break;
	}
	return FALSE;
}

/**
 * name:	DlgProc_pspMyNotes()
 * desc:	dialog procedure
 *
 * return:	0 or 1
 **/
INT CALLBACK DlgProc_pspMyNotes( HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
	return DlgProc_pspEdit( hDlg, uMsg, wParam, lParam, SET_CONTACT_MYNOTES );
}

/**
 * name:	DlgProc_pspAbout()
 * desc:	dialog procedure
 *
 * return:	0 or 1
 **/
INT CALLBACK DlgProc_pspAbout( HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
	return DlgProc_pspEdit( hDlg, uMsg, wParam, lParam, SET_CONTACT_ABOUT );
}
